﻿/* Author: Xinaris Charalambos */
/* xinaris@gmail.com */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Data;
using System.IO;
using System.Xml;

/// <summary>
/// Summary description for YahooWeather
/// </summary>
public class YahooWeather
{
		
        string url_resolve_place = "http://yahoowidget.weather.com/weather/search/search?where=";
        string yahoo_url_weather = "http://yahoowidget.weather.com/weather/local/";

        string place = "";
        string id_place = "";

        public YahooWeather(string query_place)
        {
            place = query_place;
        }

        public YahooWeather()
        {
        }

        public string resolvePlace()
        {
            string requested_url = url_resolve_place + place;
            //Console.WriteLine(requested_url);
            HttpWebRequest webRequest = WebRequest.Create(requested_url) as HttpWebRequest;
            //webRequest.Method = "GET";

            StreamReader responseReader = new StreamReader(
                webRequest.GetResponse().GetResponseStream()
            );
            string responseData = responseReader.ReadToEnd();

            //Console.WriteLine("Got the xml response. \n");

            return responseData;
        }

        public string getWeather(string result)
        {
            string finalurl = yahoo_url_weather + result + "?cc=*&dayf=5&ut=c&ud=k&us=k&up=m&ur=m&prod=bd_select&par=yahoowidgetxml";
            Console.WriteLine("finalurl: " + finalurl);
            HttpWebRequest webRequest = WebRequest.Create(finalurl) as HttpWebRequest;

            StreamReader responseReader = new StreamReader(
                webRequest.GetResponse().GetResponseStream()
            );
            string responseData = responseReader.ReadToEnd();

            return responseData;

        }

        public string getWeatherIcon(string iconVal, string nowTime, string sunriseTime, string sunsetTimne)
        {
            DateTime nt = Convert.ToDateTime(nowTime);
            DateTime rt = Convert.ToDateTime(sunriseTime);
            DateTime st = Convert.ToDateTime(sunsetTimne);
            switch (iconVal)
            {
                case "0":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/1d.png";
                case "1":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/1d.png";
                case "2":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/2d.png";
                case "3":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/3d.png";
                case "4":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/4d.png";
                case "5":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/5d.png";
                case "6":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/6d.png";
                case "7":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/7d.png";
                case "8":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/8d.png";
                case "9":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/9d.png";
                case "10":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/10d.png";
                case "11":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/11d.png";
                case "12":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/12d.png";
                case "13":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/13d.png";
                case "14":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/14d.png";
                case "15":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/15d.png";
                case "16":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/16d.png";
                case "17":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/17d.png";
                case "18":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/18d.png";
                case "19":
                    if (nt >= rt)
                        return "http://l.yimg.com/a/i/us/nws/weather/gr/19d.png";
                    else if (nt >= st)
                        return "http://l.yimg.com/a/i/us/nws/weather/gr/19n.png";
                    break;
                case "20":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/20d.png";
                case "21":
                    if (nt >= rt)
                        return "http://l.yimg.com/a/i/us/nws/weather/gr/21d.png";
                    else if (nt >= st)
                        return "http://l.yimg.com/a/i/us/nws/weather/gr/21n.png";
                    break;
                case "22":
                    if (nt >= rt)
                        return "http://l.yimg.com/a/i/us/nws/weather/gr/22d.png";
                    else if (nt >= st)
                        return "http://l.yimg.com/a/i/us/nws/weather/gr/22n.png";
                    break;
                case "23":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/23d.png";
                case "24":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/24d.png"; 
                case "25":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/25d.png";
                case "26":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/26d.png";
                case "27":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/27n.png";
                case "28":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/28d.png";
                case "29":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/29n.png";
                case "30":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/30d.png";
                case "31":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/31n.png";
                case "32":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/32d.png";
                case "33":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/33n.png";
                case "34":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/33d.png";
                case "35":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/35d.png";
                case "36":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/36d.png";
                case "37":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/37d.png";
                case "38":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/38d.png";
                case "39":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/39d.png";
                case "40":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/40d.png";
                case "41":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/41d.png";
                case "42":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/42d.png";
                case "43":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/43d.png";
                case "44":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/30d.png";
                case "45":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/45d.png";
                case "46":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/46d.png";
                case "47":
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/47d.png";
                case "3200": // Not available
                    return "http://l.yimg.com/a/i/us/nws/weather/gr/44d.png";
            }
            return "";
        }
}

